from selenium import webdriver
from urllib import request
import random,time,json

defalut_keywords=["郑云龙 蒙面唱将","郑云龙 蒙面唱将","郑云龙 蒙面唱将","郑云龙 蒙面唱将","郑云龙新粉指南","郑云龙新粉指南","郑云龙新粉指南","郑云龙新粉指南",
          "郑云龙 变身怪医","郑云龙 三星堆","郑云龙采访","郑云龙金丝眼镜", "郑云龙 长发", "郑云龙人间天使", "郑云龙 人生选择题", "郑云龙烟熏妆", "郑云龙如果爱"]

def GetKeyWords():
        m_access_token = request.urlopen('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='+'wx86c73ad7800bd92a'+'&secret='+'d9ea50397c9c181400a4c83164511e59')
        m_access_token = m_access_token.read().decode('utf-8')
        token_json = json.loads(m_access_token)
        m_keywords = request.urlopen('https://api.weixin.qq.com/tcb/invokecloudfunction?access_token='+token_json['access_token']+'&env='+'dlkeywords-89y3z'+'&name='+'GetKeyWords')
        m_keywords = m_keywords.read().decode('utf-8')
        keywords_json = json.loads(m_keywords)
        cloud_keywords_json = json.loads(keywords_json['resp_data'])
        cloud_keywords = cloud_keywords_json['keywords']
        if len(cloud_keywords)>0:
                print("\n已加载最新词条库：\n")
                print(cloud_keywords)
                print("(重复的词条有更大可能性刷到)")
                return(cloud_keywords)
        else:
                return(defalut_keywords)
        
keywords=GetKeyWords()
browser = webdriver.Chrome()
browser.get("https://s.weibo.com/weibo?q=%E9%83%91%E4%BA%91%E9%BE%99&wvr=6&b=1&Refer=SWeibo_box")

def Search(keyword):    
        content_textarea = browser.find_element_by_xpath("//input[@type='text']").clear()
        content_textarea = browser.find_element_by_xpath("//input[@type='text']").send_keys(keyword)
        post_button = browser.find_element_by_css_selector("[node-type='submit']").click()
       
def browserquit(self):
        browser.quit()

for i in range(1,100000000):
        keyword=keywords[random.randint(0,len(keywords)-1)]
        Search(keyword)
        print(str(i)+"  "+keyword)
        time.sleep(15.001)
